package org.jmeter.tcp.helmet;

import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.gui.util.HorizontalPanel;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextArea;
import org.apache.jorphan.gui.JLabeledTextField;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.jmeter.tcp.helmet.protobuf.util.Helmet2ServerMsgBuilder;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * @author liangsu
 * @version v1.0
 * @Description
 * @Date 2020/11/13 15:46
 * @since
 */
public class HelmetClientGui extends AbstractSamplerGui {

    JLabeledTextField ip = new JLabeledTextField("ip:");
    JLabeledTextField port = new JLabeledTextField("port:");
    JLabeledTextField version = new JLabeledTextField("version:");
    JLabeledTextField devId = new JLabeledTextField("devId:");
    JComboBox msgType = new JComboBox();
    JLabeledTextArea msgBody = new JLabeledTextArea("message");

    private static final Logger log = LoggingManager.getLoggerForClass();

    public HelmetClientGui(){
        init();
    }

    protected void init() {
        setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH); // Add the standard title

        JPanel mainPanel = new VerticalPanel();

        mainPanel.add(createMainPanel());

        add(mainPanel);
    }

    public JPanel createMainPanel(){
        VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.setBorder(makeBorder());

        HorizontalPanel line1 = new HorizontalPanel();
        line1.add(ip);
        line1.add(port);
        mainPanel.add(line1);

        HorizontalPanel line2 = new HorizontalPanel();

        HorizontalPanel msgTypePanel = new HorizontalPanel();
        JLabel msgTypeLabel = new JLabel("msgType：");
        msgTypePanel.add(msgTypeLabel);
        // 消息类型
        List<String> msgTypes = Helmet2ServerMsgBuilder.getMsgTypes();
        for (String type : msgTypes) {
            msgType.addItem(type);
        }
        msgTypePanel.add(msgType);

        line2.add(msgTypePanel);
        line2.add(version);
        mainPanel.add(line2);

        mainPanel.add(devId);

        msgBody.setPreferredSize(new Dimension(800, 400));
        mainPanel.add(msgBody);
        return mainPanel;
    }


    @Override
    public String getLabelResource() {
        return this.getClass().getSimpleName();
    }

    /**
     * 静态的标签名称
     * @return
     */
    @Override
    public String getStaticLabel() {
        return "helmet client";
    }

    @Override
    public TestElement createTestElement() {
        //System.out.println("createTestElement");

        HelmetClientSampler helmetClientSampler = new HelmetClientSampler();

        modifyTestElement(helmetClientSampler);


        return helmetClientSampler;
    }

    /**
     * 将界面的数据保存到sampler
     * @param testElement
     */
    @Override
    public void modifyTestElement(TestElement testElement) {
        super.configureTestElement(testElement);

        HelmetClientSampler sampler = (HelmetClientSampler) testElement;
        sampler.setIp(ip.getText());
        if(StringUtils.isNotBlank(port.getText())){
            sampler.setPort(Integer.parseInt(port.getText()));
        }
        sampler.setMsgType(msgType.getSelectedItem().toString());
        sampler.setMsg(msgBody.getText());
        sampler.setDevId(devId.getText());
        sampler.setVersion(version.getText());

        //System.out.println("modifyTestElement");
    }

    /**
     * 把Sampler中的数据加载到界面中
     * @param element
     */
    @Override
    public void configure(TestElement element) {
        super.configure(element);

        if(element instanceof HelmetClientSampler){
            HelmetClientSampler sampler = (HelmetClientSampler) element;
            ip.setText(sampler.getIp());
            port.setText(sampler.getPort().toString());
            msgType.setSelectedItem(sampler.getMsgType());
            msgBody.setText(sampler.getMsg());
            version.setText(sampler.getVersion());
            devId.setText(sampler.getDevId());
        }

        //System.out.println("configure");
    }
}
